class Access {
    Role role
    Employee emp
    String userName
    String password
    Area area
    Container container

    //static belongsTo = [Employee, Area, Container]

    static constraints = {

    }
    public Integer haveAccessToController(){
        return this.container.id
    }
    public Boolean hasAdminAuth(){
        return role.level == 1
    }
    public Boolean hasManagerAuth(){
        return role.level == 2
    }
    public Boolean checkAccess(String controller, String action, Long id){
        if(controller.equalsIgnoreCase("area")){
            switch(action){
                case "show" :
                    if(this.role.level == 1){
                        return this.container.equals(Container.findById(id))
                    }
                    if(this.role.level == 2){
                        def a = Area.get(id)
                        return (this.area.id.equals(a.id) && this.container.id.equals(a.container.id))
                    }
                    else return false
                    break
                case "create" :
                    if(this.role.level == 1){
                        return this.container.equals(Container.findBy(id))
                    }
                    else return false
                    break
                case "edit" :
                    if(this.role.level == 1){
                        def area = Area.get(id)
                        return area.container.id == this.container.id
                    }
                    if(this.role.level == 2){
                        def area = Area.get(id)
                        return (this.area.id.equals(area.id) && this.container.id.equals(area.container.id))
                    }
                    break
                case "delete" :
                if(this.role.level == 1){
                    return this.container.equals(Container.findBy(id))
                }
                default : false
                }
            }
            else{
                return false
            }
        }
    //String toString(){"${this.emp} : ${this.role}"}
    String toString(){"${this.role}"}
}
